import re
import pandas as pd
import jieba

def use_txt():
    raw = pd.read_csv("金庸-射雕英雄传txt精校版.txt",
    names = ['txt'], sep ='aaa', encoding ="GBK" ,engine='python')

    def h_head(tmpstr):
        return tmpstr[:1]
    def h_mid(tmpstr):
        return tmpstr.find("回 ")

    raw['head'] = raw.txt.apply(h_head)
    raw['mid'] = raw.txt.apply(h_mid)
    raw['len'] = raw.txt.apply(len)

    chapnum = 0
    for i in range(len(raw)):
        if raw['head'][i] == "第" and raw['mid'][i] > 0 and raw['len'][i] < 30:
            chapnum += 1
        if chapnum >= 40 and raw['txt'][i] == "附录一：成吉思汗家族":
            chapnum = 0
        raw.loc[i, 'chap'] = chapnum


    del raw['head']
    del raw['mid']
    del raw['len']

    tmpchap = raw[raw['chap'] == 1].copy()
    tmpchap.reset_index(drop=True, inplace=True)
    tmpchap['paraidx'] = tmpchap.index


    list=[]
    for i in tmpchap['paraidx']:
        tmppara=tmpchap[tmpchap['paraidx'] == i].copy()
        tmpstr=tmppara.txt[i]
        sentences=re.findall('(.*?[。！？ ；：])', tmpstr)
        list.append(sentences)


    length=[]
    for i in sentences:
        length.append(len(i))
    maxpara_index=length.index(max(length))
    texts1=sentences[maxpara_index]
    return texts1

#用词库
def word_stock(sentence):
    dict = "金庸武侠地名词库.txt"
    jieba.load_userdict(dict)
    cut_result=jieba.lcut(sentence)
    return cut_result
#
#加载停用词表
def word_stop(sentence):
    stop_list=[]
    with open("停用词.txt",'r',encoding='utf-8') as f:
        for word in f:
            if len(word)>0:
                stop_list.append(word.strip())
    return stop_list

#去停用词并且使用词库
def all_word(para):
    stop_list = []
    for word in word_stock(para):
        if word not in stop_list:
            stop_list.append(word)
    return stop_list


def main():
    para=use_txt()
    print("1.使用词库:\n", word_stock(para))
    print("2.使用停用词以及词库:\n", all_word(para))
    print("3.不使用停用词以及词库:\n", jieba.lcut(para))



if __name__=="__main__":
    main()
